

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;

/**
 * Testapplication to show the sorting and filtering features of a Swing JTable
 * using the TableRowSorter and the RowFilter classes
 * @author GEORES
 *
 */
public class TableTestFrame extends JFrame {
	
	private JTable table; 
    private JTextField filterText;
    //the row sorter handles all the sorting features of our application 
    private TableRowSorter<DefaultTableModel> sorter;

    /**
     * Constructor -> calls the init method
     * @param string
     */
	public TableTestFrame(String string) {
		super(string);
		init();
		
	}

	/**
	 * all the frame initialization is done here
	 * 
	 */
	private void init() {
		//create a new Panel
		JPanel mainPanel = new JPanel();
		//set a nice layout
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		//the columns ouf our JTable
        String[] columnNames = {"Game",
                "Genre",
                "System",
                "Price"};
        //creating a default TableModel with our columns
		DefaultTableModel model = new DefaultTableModel(columnNames,0);
		//adding data to tor table model
		addData(model);
		//Create a table with a sorter.
        sorter = new TableRowSorter<DefaultTableModel>(model);
        table = new JTable(model);
        table.setRowSorter(sorter);
        //It's possible to set Comparators for each column of the table
        //sorter.setComparator(0, new MyGameComparator());
        table.setPreferredScrollableViewportSize(new Dimension(500, 100));
        table.setFillsViewportHeight(true);
      //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(table);
 
        //Add the scroll pane to this panel.
        mainPanel.add(scrollPane);
        
      //Create a separate form for filterText and statusText
        JPanel form = new JPanel(new FlowLayout());
        JLabel l1 = new JLabel("Filter Text:", SwingConstants.TRAILING);
        form.add(l1);
        //Creating the Textfield
        filterText = new JTextField(50);
        //Whenever filterText changes, invoke newFilter.
        //Creating an anonymous inner class object using the DocumentListener interface
        filterText.getDocument().addDocumentListener(
                new DocumentListener() {
                	//Every Event leads to the creation of a new filtered Table
                    public void changedUpdate(DocumentEvent e) {
                        newFilter();
                    }
                    public void insertUpdate(DocumentEvent e) {
                        newFilter();
                    }
                    public void removeUpdate(DocumentEvent e) {
                        newFilter();
                    }
                });
        l1.setLabelFor(filterText);
        form.add(filterText);
        mainPanel.add(form);
        this.setContentPane(mainPanel);
		
	}
	
	/**
	 * adding some data to our table
	 * @param model the default TableModel of our JTable
	 */
	private void addData(DefaultTableModel model) {
		Game g = new Game("Alien III", GenreType.actionadventure, SystemType.ps3, 49.90);
		Game g1 = new Game("Rayman origins", GenreType.jumpnrun, SystemType.wiiU, 79.90);
		Game g2 = new Game("Diablo III", GenreType.actionadventure, SystemType.xbox360, 49.90);
		Game g3 = new Game("FIFA 14", GenreType.sports, SystemType.ps3, 59.90);
		Game g4 = new Game("The last of us", GenreType.adventure, SystemType.ps3,69.90);
		model.addRow(new Object[]{g,g.getGenre(),g.getType(),g.getPriceInEuro()});
		model.addRow(new Object[]{g1,g1.getGenre(),g1.getType(),g1.getPriceInEuro()});
		model.addRow(new Object[]{g2,g2.getGenre(),g2.getType(),g2.getPriceInEuro()});
		model.addRow(new Object[]{g3,g3.getGenre(),g3.getType(),g3.getPriceInEuro()});
		model.addRow(new Object[]{g4,g4.getGenre(),g4.getType(),g4.getPriceInEuro()});
		
		
	}

	/**
     * Update the row filter regular expression from the expression in
     * the text box.
     */
    private void newFilter() {
        RowFilter<DefaultTableModel, Object> rf = null;
        //If current expression doesn't parse, don't update.
        try {
        	//Using the Filter for all of our columns 0....3
            rf = RowFilter.regexFilter(filterText.getText(), new int[]{0,1,2,3});
            //the following expression can sort our price Table with values lesser or greater than the value of
            //the JTextField
            //rf = RowFilter.numberFilter(ComparisonType.AFTER, Double.valueOf(filterText.getText()), 3);
        } catch (java.util.regex.PatternSyntaxException e) {
            return;
        }catch(NumberFormatException e){
        	return;
        }
        sorter.setRowFilter(rf);
    }

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//Create and set up the window.
		TableTestFrame frame = new TableTestFrame("TableTestFrame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
        //Display the window.
        frame.pack();
        frame.setVisible(true);

	}

}
